package cn.dubito.mmall.controller.backend;

import cn.dubito.mmall.commons.Const;
import cn.dubito.mmall.commons.ResponseCode;
import cn.dubito.mmall.commons.ServerResponse;
import cn.dubito.mmall.pojo.User;
import cn.dubito.mmall.service.CategoryService;
import cn.dubito.mmall.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * @author liliang
 * @description: 分类接口
 * @date 2018/1/28.
 */
@RequestMapping("/manage/category/")
@Controller
public class CategoryManageController {

    @Autowired
    private UserService userService;

    @Autowired
    private CategoryService categoryService;

    @RequestMapping("add_category.do")
    @ResponseBody
    public ServerResponse addCategory(HttpSession session, String categoryName, @RequestParam(value = "parentId", defaultValue = "0") int parentId) {
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if (user == null) {
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(), "用户未登录,请登录!");
        }
        // 是否是管理员
        if (userService.checkAdminRole(user).isSuccess()) {
            // 是管理员,添加分类操作
            return categoryService.addCategory(categoryName, parentId);
        } else {
            return ServerResponse.createByErrorMessage("没有管理员权限,无法操作");
        }
    }
}
